package com.zzq.guli.service.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzq.guli.service.base.result.R;
import com.zzq.guli.service.edu.entity.*;
import com.zzq.guli.service.edu.service.CourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ZZQ
 * @since 2024-04-12
 */

//@CrossOrigin
@RestController
@RequestMapping("admin/edu/course")
public class CourseController {


    @Autowired
    CourseService courseService;

    @RequestMapping("saveCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm){

       String courseId=courseService.saveCourseInfo(courseInfoForm);
       return R.ok().data("courseId",courseId);
    }



    @GetMapping("list/{page}/{limit}")
    public R getCourseList(@PathVariable Long page,
                           @PathVariable Long limit,
                           CourseQuery courseQuery){

        Page<CourseVo> courseVoList= courseService.selectPage(page,limit,courseQuery);

        return R.ok().data("courseList",courseVoList);

    }


    @GetMapping("getCourseById/{courseId}")
    public R getCourseById(@PathVariable String courseId){
        Course byId = courseService.getById(courseId);
        return R.ok().data("courseInfo",byId);

    }

    @RequestMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm){

        courseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }


    @DeleteMapping("removeById/{courseId}")
    public R removeById(@PathVariable String courseId){
        courseService.removeCourseById(courseId);

        return R.ok().message("删除课程成功");

    }
    @ApiOperation("根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){

        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据ID获取课程发布信息")
    @GetMapping("course-publish/{id}")
    public R getCoursePublishVoById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){

        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        if (coursePublishVo != null) {
            return R.ok().data("item", coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

}

